package com.example.demofdfs.test2;

import com.example.demofdfs.example.link.DoubleLinked;
import com.example.demofdfs.example.link.Links;

public class NodeTest {

	public static void main(String[] args) {
		Links<Node> links = new DoubleLinked<>();
		Node root = new Node(index());
		init(root, 4);
		
//		System.out.println(root);
		root.access(links);
		System.out.println(links);
		System.out.println(links.size());
	}

	
	public static void init(Node root, int depth) {
		if(depth == 1)
			return;
		
		Node l = new Node(index());
		Node r = new Node(index());
		root.setLeftChild(l);
		root.setRightChild(r);
		
		init(l, depth - 1);
		init(r, depth - 1);
	}

	public static int index;
	public static int index() {
		return index++;
	}
}


class Node {
	
	private Node leftChild;
	private Node rightChild;
	private int num = -1;
	
	public Node() {
		super();
	}
	public Node(int num) {
		super();
		this.num = num;
	}
	public Node getLeftChild() {
		return leftChild;
	}
	public void setLeftChild(Node leftChild) {
		this.leftChild = leftChild;
	}
	public Node getRightChild() {
		return rightChild;
	}
	public void setRightChild(Node rightChild) {
		this.rightChild = rightChild;
	}
	
	public void access() {
		access(null);
	}
	
	public void access(Links<Node> links) {
		
		System.out.println(this);
		if(links != null)
			links.add(this);
		
		Node lc = this.leftChild;
		Node rc = this.rightChild;
		if(lc != null)
			lc.access(links);
		
		if(rc != null)
			rc.access(links);
		
	}
	@Override
	public String toString() {
		return num + "";
	}
	
	
}
